<?php
/* -------------------------------------------------------------------------------------
* 	ID:						$Id: default.php 400 2014-01-18 16:03:51Z phone.mueller@googlemail.com $
* 	Letzter Stand:			$Revision: 400 $
* 	zuletzt geaendert von:	$Author: siekiera $
* 	Datum:					$Date: 2014-01-18 16:03:51 +0000 (Sat, 18 Jan 2014) $
*
* 	SEO:mercari by Siekiera Media
* 	http://www.seo-mercari.de
*
* 	Copyright (c) since 2011 SEO:mercari
* --------------------------------------------------------------------------------------
* 	based on:
* 	(c) 2000-2001 The Exchange Project  (earlier name of osCommerce)
* 	(c) 2002-2003 osCommerce - www.oscommerce.com
* 	(c) 2003     nextcommerce - www.nextcommerce.org
* 	(c) 2005     xt:Commerce - www.xt-commerce.com
*
* 	Released under the GNU General Public License
* ----------------------------------------------------------------------------------- */

$default_smarty = new Smarty();
$default_smarty->assign('tpl_path', 'templates/'.CURRENT_TEMPLATE.'/');
$default_smarty->assign('linkurl', str_replace('/', '', $_REQUEST['linkurl']));
$default_smarty->assign('session', session_id());
$main_content = '';

require_once(DIR_FS_INC.'inc.add_js_plugin.php');
require_once(DIR_FS_INC.'inc.customer_greeting.php');
require_once(DIR_FS_INC.'inc.get_path.php');
require_once(DIR_FS_INC.'inc.check_categories_status.php');
require_once(DIR_FS_INC.'functions.image.php');
require_once(DIR_FS_INC.'functions.string.php');

$default_smarty->assign('BOXES_product_listing_top', $smarty->getTemplateVars('BOXES_product_listing_top'));

if(check_categories_status($current_category_id) >= 1) {
	$error = CATEGORIE_NOT_FOUND;
	include(DIR_WS_MODULES.FILENAME_ERROR_HANDLER);

}
else {
  if(GROUP_CHECK == 'true') {
    $group_check_c = "AND c.group_permission_".$_SESSION['customers_status']['customers_status_id']." = 1 "; // Kategorie
    $group_check_p = "AND p.group_permission_".$_SESSION['customers_status']['customers_status_id']." = 1 "; // Produkt
  }
	
  $total_ids = '';
  $fsk_lock = '';

  if($_SESSION['customers_status']['customers_fsk18_display'] == '0')
    $fsk_lock = ' AND p.products_fsk18 !=1';

  //echo_e($category_depth);
  if ($category_depth == 'nested' || $category_depth == 'empty') {
    // Anzeige Kategorien, und Produkte, wenn vorhanden

		$category_query = $db->db_query("
      SELECT
        cd.categories_description,
        cd.categories_name,
        cd.categories_heading_title,
        c.categories_template,
        c.products_listing_style,
        c.categories_image,
        cd.categories_pic_alt
      FROM
        ".TABLE_CATEGORIES." c, ".TABLE_CATEGORIES_DESCRIPTION." cd
      WHERE
        c.categories_id = '".$current_category_id."'
      AND
        cd.categories_id = '".$current_category_id."'
      ".$group_check_c."
      AND
        cd.language_id = '".(int)$_SESSION['languages_id']."'", false);

    $category_description_tags = parseTags($category_query->fields['categories_description'], true);
    //echo_e($category_description_tags, 'E');
    $image = '';
    if($category_query->fields['categories_image'] != '')
    $image = DIR_WS_IMAGES.'categories/'.$category_query->fields['categories_image'];

    $default_smarty->assign('CATEGORIES_NAME', $category_query->fields['categories_name']);
    $default_smarty->assign('CATEGORIES_HEADING_TITLE', $category_query->fields['categories_heading_title']);
    $default_smarty->assign('CATEGORIES_IMAGE', $image);
    $default_smarty->assign('CATEGORIES_DESCRIPTION', $category_query->fields['categories_description']);
    $default_smarty->assign('CATEGORIES_DESCRIPTION_TAGS', $category_description_tags);
    $default_smarty->assign('CATEGORIES_IMAGE_ALT', $category_query->fields['categories_pic_alt'] !='' ? $category_query->fields['categories_pic_alt'] : $category_query->fields['categories_name']);
    $default_smarty->assign('CATEGORIES_IMAGE_DIMENSION', get_img_size($image));

    if (isset($cPath)) $arr_cPath = explode('_', $cPath);
    $categories_sub_query = $db->db_query("
      SELECT
        cd.categories_description,
        c.categories_id,
        cd.categories_name,
        cd.categories_heading_title,
        c.categories_image,
        cd.categories_pic_alt,
        c.parent_id
      FROM
        ".TABLE_CATEGORIES." c,
        ".TABLE_CATEGORIES_DESCRIPTION." cd
      WHERE
        c.categories_status = '1'
        AND c.parent_id = '"
        . (isset($cPath) ? end($arr_cPath) : $current_category_id) ."'
        AND c.categories_id = cd.categories_id
      ".$group_check_c."
        AND cd.language_id = '".(int)$_SESSION['languages_id']."'
      ORDER BY
        sort_order, cd.categories_name", true);
	
//echo_e($categories_sub_query->sql);
    $rows = 0;
    $image_paths = getImagePaths(DIR_WS_CATEGORIES_IMAGES, 'DIR_WS');
    while(!$categories_sub_query->EOF) {
      $rows ++;
      $cPath_new = category_link($categories_sub_query->fields['categories_id']);

      $image = '';
      if ($categories_sub_query->fields['categories_image'] != '')
        $image = DIR_WS_IMAGES.'categories/'.$categories_sub_query->fields['categories_image'];
      $CATEGORIES_IMAGE_EXT = array();
      if ($image != '') {
        foreach($image_paths as $image_path) {
          if ($image_path != 'categories_original_images') {
            $image_path_arr = explode('_', $image_path);
            $CATEGORIES_IMAGE_EXT[strtoupper($image_path_arr[1])] = constant('DIR_WS_'.strtoupper($image_path)).$categories_sub_query->fields['categories_image'];
          }
        }
      }

      $categories_description_tags = parseTags($categories_sub_query->fields['categories_description'], true);
      $categories_content[] = array (
        'CATEGORIES_NAME' => $categories_sub_query->fields['categories_name'],
        'CATEGORIES_HEADING_TITLE' => $categories_sub_query->fields['categories_heading_title'],
        'CATEGORIES_IMAGE' => $image,
        'CATEGORIES_IMAGE_EXT' => $CATEGORIES_IMAGE_EXT,
        'CATEGORIES_IMAGE_ALT' => ($categories_sub_query->fields['categories_pic_alt'] !='' ? $categories_sub_query->fields['categories_pic_alt'] : $categories->fields['categories_name']),
        'CATEGORIES_IMAGE_DIMENSION' => get_img_size($image),
        'CATEGORIES_LINK' => href_link(FILENAME_DEFAULT, $cPath_new),
        'CATEGORIES_DESCRIPTION' => str_replace('"', '\'', $categories_sub_query->fields['categories_description']));

      $categories_sub_query->MoveNext();
    }
    
    //echo_e($categories_content, 'E');
    $new_products_category_id = $current_category_id;
    include(DIR_WS_MODULES.FILENAME_NEW_PRODUCTS);

    if(PRODUCT_LISTING_SHOW_PRODUCTS == 'true')
      include(DIR_WS_MODULES.'product_categorie_listing.php');

    $default_smarty->assign('language', $_SESSION['language']);
    $default_smarty->assign('CATEGORIES_LIST', $module_categories);
    $default_smarty->assign('module_content', $categories_content);
    //echo_e($default_smarty->getTemplateVars(), 'E');
    include(DIR_WS_MODULES.'slider.php');

    if($category_query->fields['categories_template'] == ''
      || $category_query->fields['categories_template'] == 'default') {
      $files = array ();
      if($dir = opendir(DIR_FS_CATALOG.'templates/'.CURRENT_TEMPLATE.'/module/categorie_listing/')) {
        while(($file = readdir($dir)) !== false) {
          if(is_file(DIR_FS_CATALOG.'templates/'.CURRENT_TEMPLATE.'/module/categorie_listing/'.$file) && (substr($file, -5) == '.html') && ($file != 'index.html') && (substr($file, 0, 1) !='.'))
          $files[] = $file;
        }
        closedir($dir);
      }
      sort($files);
      $category_query->fields['categories_template'] = $files[0];
    }

    //echo_e($category->fields['categories_template'], 'E');
    $default_smarty->caching = false;
    $main_content = $default_smarty->fetch(CURRENT_TEMPLATE.'/module/categorie_listing/'.$category_query->fields['categories_template']);
    $smarty->assign('main_content', $main_content);
  }

  else if ($category_depth == 'products' || ($_GET['brand'] && $_GET['brand'] !='')) {

    if(isset($_GET['brand']))
  		$sorting_sql = $db->db_query("SELECT products_listing_style, products_sorting, products_sorting2 FROM ".TABLE_CATEGORIES." WHERE categories_id='".$new_products_category_id."'", true);
    else
	    $sorting_sql = $db->db_query("SELECT products_listing_style, products_sorting, products_sorting2 FROM ".TABLE_CATEGORIES." WHERE categories_id = '".$current_category_id."'", true);

    $products_listing_style = $sorting_sql->fields['products_listing_style'];
		if($sorting_sql->fields['products_sorting'] == '')
			$sorting_sql->fields['products_sorting'] = 'pd.products_name';

    switch ($_GET['multisort']) {
      case 'specialprice':
        $sorting = ' ORDER BY s.specials_price_'.$_SESSION['customers_status']['customers_status_id'].' DESC ';
        $field = ' INNER JOIN '.TABLE_SPECIALS.' AS s ON (p.products_id = s.products_id) ';
        break;
      case 'new_asc':
        $sorting = ' ORDER BY p.products_date_added ASC';
        break;
      case 'new_desc':
        $sorting = ' ORDER BY p.products_date_added DESC';
        break;
      case 'name_asc':
        $sorting = ' ORDER BY pd.products_name ASC';
        break;
      case 'name_desc':
        $sorting = ' ORDER BY pd.products_name DESC';
        break;
      case 'price_asc':
        $sorting = ' ORDER BY p.products_price ASC';
        break;
      case 'price_desc':
        $sorting = ' ORDER BY p.products_price DESC';
        break;
      case 'brand_asc':
        $sorting = ' ORDER BY b.brand_name ASC';
        break;
      case 'brand_desc':
        $sorting = ' ORDER BY b.brand_name DESC';
        break;
      default:
        $sorting = ' ORDER BY '.$sorting_sql->fields['products_sorting'].' '.$sorting_sql->fields['products_sorting2'].' ';
    }

    $listing_sql = "
      SELECT DISTINCT
        p.products_fsk18, p.products_shippingtime, p.products_model,
        pd.products_name, p.products_ean, p.products_isbn,
        p.products_list_overlay, p.products_price, p.products_tax_class_id,
        b.brand_name,
        p.products_quantity, p.products_image, p.products_weight,
        pd.products_short_description, pd.products_description, pd.products_img_alt,
        p.products_id, p.brand_id, p.products_price,
        p.products_vpe, p.products_vpe_status, p.products_vpe_value,
        p.products_discount_allowed, p.products_tax_class_id
      FROM
        ".TABLE_PRODUCTS." p
        inner join ".TABLE_PRODUCTS_DESCRIPTION." pd on pd.products_id = p.products_id
        left join ".TABLE_BRAND." b on b.brand_id = p.brand_id
        inner join ".TABLE_PRODUCTS_TO_CATEGORIES." p2c on p2c.products_id = p.products_id
        ".$field."
      WHERE
        p.products_status = '1'
        and pd.language_id = '".(int)$_SESSION['languages_id']."'
        ". (isset($_GET['brand']) && $_GET['brand'] != ''
          ? "and b.brand_id = '".(int)$_GET['brand']."'" : "") ."
        ". (isset($_GET['filter_id']) && $_GET['filter_id'] != ''
          ? "and p2c.categories_id = '".(int)$_GET['filter_id']."'" : "") ."
        ". ((!isset($_GET['brand']) || $_GET['brand'] == '')
              && (!isset($_GET['filter_id']) || $_GET['filter_id'] == '')
          ? "and p2c.categories_id = '".$current_category_id."'" : "") ."
        ".$filter_products."
        ".$group_check_p."
        ".$fsk_lock
      .$sorting;

		$total_count = $db->db_query("
      SELECT
        p.products_id
      FROM
        ".TABLE_PRODUCTS." p
        inner join ".TABLE_PRODUCTS_TO_CATEGORIES." p2c on p2c.products_id = p.products_id
        ".$field."
      WHERE
        p.products_status = '1'
        ". (isset($_GET['brand']) && $_GET['brand'] != ''
          ? "and b.brand_id = '".(int)$_GET['brand']."'" : "") ."
        ". (isset($_GET['filter_id']) && $_GET['filter_id'] != ''
          ? "and p2c.categories_id = '".(int)$_GET['filter_id']."'" : "") ."
        ". ((!isset($_GET['brand']) || $_GET['brand'] == '')
              && (!isset($_GET['filter_id']) || $_GET['filter_id'] == '')
          ? "and p2c.categories_id = '".$current_category_id."'" : "") ."
        ".$filter_products." "
      .$group_check_p
      .$fsk_lock, true);

    $total_ids = $total_count->_numOfRows;
    include (DIR_WS_MODULES.FILENAME_PRODUCT_LISTING);
  }

  else { // Startseite
		if (GROUP_CHECK == 'true')
			$group_check = " AND group_ids LIKE '%c_".$_SESSION['customers_status']['customers_status_id']."_group%'";

		$shop_content = $db->db_query("
      SELECT
        content_title,
        content_heading,
        content_text,
        content_file
      FROM
        ".TABLE_CONTENT_MANAGER."
      WHERE
        content_group = '5'
        ".$group_check."
        AND
        languages_id='".$_SESSION['languages_id']."'", true);

		$default_smarty -> assign('title', utf8_decode(($shop_content->fields['content_heading'])));
		include (DIR_WS_INCLUDES.FILENAME_CENTER_MODULES);

		if ($shop_content->fields['content_file'] != '') {
			ob_start();
			if (strpos($shop_content->fields['content_file'], '.txt'))
				echo '<pre>';

			include (DIR_FS_CATALOG.'media/content/'.$shop_content->fields['content_file']);

			if (strpos($shop_content->fields['content_file'], '.txt'))
				echo '</pre>';
			
			$shop_content->fields['content_text'] = ob_get_contents();
			ob_end_clean();
		}
		$default_smarty->assign('text', str_replace('{$greeting}', customer_greeting(), $shop_content->fields['content_text']));
		$default_smarty->assign('language', $_SESSION['language']);
		
		include(DIR_WS_MODULES.'slider.php');
		
		if(!CacheCheck()) {
      $default_smarty->caching = false;
      $main_content = $default_smarty->fetch(CURRENT_TEMPLATE.'/module/main_content.html');
    }
    else {
      $default_smarty->caching = true;
      $default_smarty->cache_lifetime = CACHE_LIFETIME;
      $default_smarty->cache_modified_check = CACHE_CHECK;
      $cache_id = $_SESSION['language'].$_SESSION['currency'].$_SESSION['customer_id'];
      $main_content = $default_smarty->fetch(CURRENT_TEMPLATE.'/module/main_content.html', $cache_id);
    }
    $smarty->assign('main_content', $main_content);
	}
	
}
//echo_e($smarty->getTemplateVars(), 'E');